package com.example.last.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.last.common.Constants;
import com.example.last.common.enums.ErrorCode;
import com.example.last.common.enums.RoleEnum;
import com.example.last.dao.UserDao;
import com.example.last.entity.Account;
import com.example.last.entity.Params;
import com.example.last.entity.User;
import com.example.last.exception.CustomException;
import com.example.last.utils.JwtTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public User login(Account account){
        //查询获取用户
        User dbUser = userDao.findByNameAndPassword(account.getUsername(),account.getPassword());

        if(ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!dbUser.getPassword().equals(account.getPassword())){
            throw new CustomException(ErrorCode.USER_ACCOUNT_ERROR);
        }
        //生成jwtToken令牌
        String token = JwtTokenUtils.genToken(dbUser.getId() + "-" + dbUser.getRole(),dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;

    }
    public void register(Account account){
        User user = new User();
        BeanUtils.copyProperties(account,user);
        add(user);
    }

    public void add(User user){
        User dbUser = userDao.selectByUsername(user.getUsername());
        if(ObjectUtil.isNotNull(dbUser)){
            throw new CustomException(ErrorCode.USER_EXIST_ERROR);
        }
        if(ObjectUtil.isEmpty(user.getPassword())){
            user.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if(ObjectUtil.isEmpty(user.getName())){
            user.setName(user.getName());
        }
        user.setRole(RoleEnum.USER.name());
        userDao.add(user);

    }

    public void updatePassword(Account account) {
        User dbUser = userDao.selectByUsername(account.getUsername());
        if(ObjectUtil.isNull(dbUser)) {
            throw new CustomException(ErrorCode.USER_NOT_EXIST_ERROR);
        }
        if(!account.getPassword().equals(dbUser.getPassword())) {
            throw new CustomException(ErrorCode.PARAM_PASSWORD_ERROR);
        }
        userDao.updatePassword(account);
    }

    public PageInfo<User> findBySearch(@Param("params") Params params){
        //开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<User> list =  userDao.findBySearch(params);
        return PageInfo.of(list);
    }


    public void update(User user){
        userDao.update(user);
    }

    public void delete(Integer id){
        userDao.delete(id);
    }
    public User findById(Integer id){
        return userDao.selectByPrimaryKey(id);
    }


    public User findByName(String username) {
        return userDao.findByName(username);
    }
}
